<template>
  <div>
    <div class="userinfo">
      <div class="headLogo">
        <img src="@/assets/headLogo.png" alt="">
      </div>
      <div class="username" v-if="userInfo.username">
        Hi {{ userInfo.username }}
      </div>
      <div class="username" v-else>
        用户名加载中
      </div>
    </div>
    <div class="useArea">
      功能区，敬请期待
    </div>
    <div class="logOut">
      <van-button round block type="danger" @click="logOut">
        退出登录
      </van-button>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import Cookies from 'js-cookie';
import { removeToken } from '../utils/auth';
import { queryUser } from '@/api/user.js';
import { showConfirmDialog } from 'vant';

const userInfo = ref({});

onMounted(() => {
  queryUser({ userId: Cookies.get('userId') }).then(res => {
    if (res.code == 200) {
      userInfo.value = res.data;
    }
  });
  /* userInfo.value = sessionStorage.getItem('userInfo');
  if (JSON.stringify(userInfo.value) == '{}') {
    queryUser({ username: Cookies.get('username') }).then(res => {
      if (res.code == 200) {
        userInfo.value = res.data;
      }
    });
  } */
});

// 退出登录
function logOut() {
  showConfirmDialog({
    title: '提示',
    message: '确认退出登录？'
  }).then(() => {
    removeToken();
    Cookies.remove('username');
    Cookies.remove('userId');
    // sessionStorage.removeItem('userInfo');
    location.reload();
  });
}

</script>

<style lang="scss" scoped>
.userinfo {
  width: 100%;
  height: 100px;
  display: flex;
  margin-bottom: 30px;

  .headLogo {
    width: 100px;
    height: 100px;

    img {
      width: 100%;
      height: 100%;
    }
  }

  .username {
    flex: 1;
    height: 100px;
    line-height: 100px;
    font-size: 20px;
    padding-left: 30px;
    overflow-x: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
}

.useArea {
  width: 100%;
  height: 200px;
}
</style>